package com.xb;

import com.xb.interceptor.LoginInterceptor;
import com.xb.util.GenerateImageCodeServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class XBConfiguration extends WebMvcConfigurerAdapter {

    /**
     * 验证码
     * @return
     */
    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new GenerateImageCodeServlet(),"/generateCode");
        return registrationBean;
    }

    // /upload/**
    @Value("${file.requestPath}")
    private String requestPath;

    // E:/java42/code/Re_xbhy
    @Value("${file.dir}")
    private String dir;

    @Autowired
    private LoginInterceptor loginInterceptor;

    /**
     * 映射/upload下的路径取磁盘找
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(requestPath).addResourceLocations("file:" + dir);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/user/checkEmail/**")
                .excludePathPatterns("/user/checkUsername/**")
                .excludePathPatterns("/user/register/**")
                .excludePathPatterns("/user/sendEmail/**")
                .excludePathPatterns("/user/login/**")
                .excludePathPatterns("/user/logout/**")
                .excludePathPatterns("/generateCode/**")
                .excludePathPatterns("/to_wxLogin/**")
                .excludePathPatterns("/wx_login/**")
                .excludePathPatterns("/generateCode/**")
                .excludePathPatterns("/index.html")
                .excludePathPatterns("/gorget.html")
                .excludePathPatterns("/register.html")
                .excludePathPatterns("/bootstrap/**")
                .excludePathPatterns("/css/**")
                .excludePathPatterns("/fonts/**")
                .excludePathPatterns("/img/**")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/vendor/**")
                .excludePathPatterns("/assets/**")
                .excludePathPatterns("/upload/**")
                .excludePathPatterns("/xb_websocket/**");
    }

    /**
     * 开启 WebSocket 支持
     * @return
     */
//    @Bean
//    public ServerEndpointExporter serverEndpointExporter(){
//        return new ServerEndpointExporter();
//    }
}
